'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:FinanceFBxdCtrl
 * @description
 * # FinanceFBxdCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('FinanceFBxdCtrl', function ($scope, $rootScope, openApi, Util, $routeParams, FormSubmit) {
        var id = $scope.process_id = $routeParams.id
        // 选择报销单明细
        $scope.showCertificate = function (index) {
            $scope.editIndex = index;
            var param = {};
            param.type = "报销费用类型";
            Util.getKeyValue(param, function (data) {
                $scope.resiTypeList = data.data
                $rootScope.$emit('showmasker');
                $scope.showCerPicker = true;
            })
        };
        $scope.deletedItem = function (index) {
            $scope.param.details.splice(index, 1);
            $scope.index = -1;
        };
        $scope.addCertificate = function () {
            $scope.picker = $scope.picker || {};
            var check = false;
            check = checkPicker($scope.picker);
            if (!check) {
                return;
            };
            if ($scope.editIndex !== -1) {
                $scope.param.details.splice($scope.index, 1, $scope.picker)
            } else {
                $scope.param.details.push($scope.picker);
            }
            // 隐藏表单
            hideCertificate();
        };
        var hideCertificate = function () {
            $scope.picker = {};
            $rootScope.$emit('closemasker');
            $scope.showCerPicker = false;
        };
        $scope.$on('closemasker', function () {
            hideCertificate()
        });
        $scope.cacelCertificate = function () {
            hideCertificate();
        };

        $scope.showSearchPicker = function () {
            $scope.searchtitle = "收款人";
            $scope.apiurl = openApi.getReimbursementHistoryRecord;
            $scope.showSearchInput = true;
            $rootScope.$emit("showmasker");
        };
        $scope.handleSearch = function (data) {
            $scope.param.receivePerson = data[0];
            $scope.param.receiveAccount = data[1];
            $scope.param.receiveBank = data[2];
            hideSearchPicker();
        };
        var hideSearchPicker = function () {
            $rootScope.$emit("closemasker");
            $scope.showSearchInput = false;
        };
        $scope.$on("closemasker", function () {
            hideSearchPicker();
        });
        // 验证是否输入完整
        var checkPicker = function (data) {
            var check = true;
            if (!data.resiType) {
                Util.Toast("请选择报销类型");
                check = false;
                return check;
            }
            if (!data.receiptsSum) {
                Util.Toast("请填写单据数量");
                check = false;
                return check;
            }
            if (!data.amount) {
                Util.Toast("请填写金额");
                check = false;
                return check;
            }
            return check;

        };
        $scope.submit = function (operationId) {

            $scope.param.reimDetails = angular.toJson($scope.param.details);
            // 底部类型end
            var options = {};
            // 流程id
            options.id = id;
            // 根据operationId判断是草稿还是提交
            options.operationId = operationId;
            options.urlParam = openApi.reimbursement;
            options.param = angular.copy($scope.param, {});
            if ($scope.param.borrows) {
                options.param.borrowJson = angular.toJson($scope.param.borrows)
            }
            delete options.param.borrows
            delete options.param.details
            FormSubmit.submit(options, $scope);
        };
        $scope.validate = function () {
            
            if (!$scope.param.receivePerson) {
                Util.Toast("请输入收款人");
                return;
            }
            if (!$scope.param.remark) {
                Util.Toast("请填写报销事由");
                return;
            }
            if ($scope.param.relatedModuleName && $scope.param.relatedModuleName !== "无") {
                if (!$scope.param.moduleNo) {
                    Util.Toast("请选择模块编号");
                    return;
                }
            }
            if ($scope.param.details.length < 1) {
                Util.Toast("请填写报销明细");
                return;
            }
            if (!$scope.validation.passvalidation) {
                Util.Toast($scope.validation.tip);
                return;
            }
            return true;
        }
        $scope.swipe = function (index) {
            $scope.index = index;
        }
        $scope.resetlist = function () {
            $scope.index = -1;
        }
        $scope.edit = function (index) {
            if ($scope.index !== -1) {
                $scope.resetlist()
                return
            }
            var current = $scope.param.details[index];
            $scope.picker = Object.assign({}, current);
            $scope.showCertificate(index)
        }
        var init = function () {
            // 获取回显的详细信息
            Util.getDetailData(id, function (data) {
                $scope.param = data;
                $scope.param.details = $scope.param.details || []
            });
        };
        // 初始化
        init();
    });